const timer = require('../model/chronograph')

let fn = async ({ data, dist }, { socket, reg, user, WARNING, NOTIFY, ERROR }, topic) => {
    try {
        let r = await reg.rooms.getRoom(dist)
        if (r.has(user)) {
            await reg.dao.newRoomMsg(dist, user.name, data)
            await r.broadcast(u => {
                u.socket.emit(topic, {
                    sender: user.toSender(),
                    data,
                })
            })
        } else {
            socket.emit(WARNING, { topic, data: '你不在房间中' })
        }
    } catch (e) {
        socket.emit(WARNING, {
            topic,
            data: e.message
        })
    }
}

module.exports = fn